package com.lzs.bootsafety.common.oss;

import com.lzs.bootsafety.business.oss.entity.SysFile;
import com.lzs.bootsafety.business.oss.service.SysConfigService;
import com.lzs.bootsafety.common.oss.Exception.FileException;
import com.lzs.bootsafety.common.oss.Exception.OssException;
import com.lzs.bootsafety.common.util.Resu;
import com.lzs.bootsafety.common.util.SpringContextAware;
import com.lzs.bootsafety.common.util.enums.SysConfig;
import com.lzs.bootsafety.common.util.oss.FileUtils;
import com.lzs.bootsafety.common.util.oss.ImageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;
/**
 *  @author  zhanshi Liu
 *  @date    2020/04/19
 *  @website www.jhmsona.cn
 *
 */
public abstract class BaseApi implements FileApi {
    protected String storageType;
    protected String newFileName;
    protected String suffix;
    protected final Logger log = LoggerFactory.getLogger(getClass());
    BaseApi(String storageType){
        this.storageType = storageType;
    }
    @Override
    /**返回文件信息*/
    public SysFile uploadFile(MultipartFile file) {
        this.check();
        if (file == null) {
            throw new OssException("[" + this.storageType + "]文件上传失败：文件不可为空");
        }
        try {
            SysFile imageInfo = ImageUtils.getInfo(file);
            SysFile sysFile = this.uploadFile(file.getInputStream(), file.getOriginalFilename());
            sysFile.setSize(imageInfo.getSize())
                    .setOriginalFileName(file.getOriginalFilename())
                    .setWidth(imageInfo.getWidth())
                    .setHeight(imageInfo.getHeight());
            return sysFile;
        } catch (IOException e) {
            throw new FileException("[" + this.storageType + "]文件上传失败：" + e.getMessage());
        }




    }
    public SysFile uploadFile(File file){
        this.check();
        if (file == null){
            throw new OssException("[" + this.storageType + "]文件上传失败：文件不可为空");
        }
        try{
            InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
            SysFile sysFile = this.uploadFile(inputStream, FileUtils.getSuffix(file));
            SysFile sysFileInfo = ImageUtils.getInfo(file);
            return sysFile.setSize(sysFileInfo.getSize()).setOriginalFileName(file.getName())
                    .setWidth(sysFileInfo.getWidth()).setHeight(sysFileInfo.getHeight());
        }catch (FileNotFoundException e){
            throw new FileException("[" + this.storageType + "]文件上传失败：" + e.getMessage());
        }
    }
    public void createNewName(String key){
        this.suffix = FileUtils.getSuffix(key);
        String fileName = key.substring(0,key.lastIndexOf("."));
        SysConfigService sysConfigService = SpringContextAware.getBean(SysConfigService.class);
        Map<String ,Object> sysConfigs = sysConfigService.getConfigs();
        String fileSuffix = String.valueOf(sysConfigs.get(SysConfig.FILE_SUFFIX.getKey()));
        /**判断文件在系统中是否允许上传*/
        if(!fileSuffix.toLowerCase().contains(this.suffix.toLowerCase())){
            throw new OssException("[" + this.storageType + "] 非法的文件[" + key + "]！目前只支持以下文件格式:"+"["+fileSuffix.toLowerCase()+"]");
        }
        this.newFileName = fileName+System.currentTimeMillis()+this.suffix;

    }
    /**由子类具体实现*/
    protected abstract void check();
}
